<?php 
class ControllerNewsCategory extends Controller{ 
	private $error = array();
		
	public function index(){
		$this->load();
		$this->getList();
	}
	
	public function insert(){
		$this->load();
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_news_category->addCategory($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');			
			$this->redirect($this->url->link('news/category', 'token=' . $this->session->data['token'].$this->url($this->getData()), 'SSL')); 
		}		
		$this->getForm();
	}
	
	public function update(){
		$this->load();		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_news_category->editCategory($this->request->get['category_id'], $this->request->post);			
			$this->session->data['success'] = $this->language->get('text_success');			
			$this->redirect($this->url->link('news/category', 'token=' . $this->session->data['token'].$this->url($this->getData()), 'SSL'));
		}
		$this->getForm();
	}
	
	public function delete() {
		$this->load();		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $category_id) {
				$this->model_news_category->deleteCategory($category_id);
			}
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('news/category', 'token=' . $this->session->data['token'].$this->url($this->getData()), 'SSL'));
		}
		$this->setList();
	}
	
	public function autocompleteCategoryName() {
		$json = array();		
		if (isset($this->request->get['filter_name']) ) {
			$this->load->model('news/category');
			$filter_name = $this->request->get['filter_name'];			
			$results = $this->model_news_category->getAutocompleteCategoryName($filter_name);			
			foreach ($results as $result) {
				$json[] = array(
					'category_id' 	=> $result['category_id'],
					'category_name' => html_entity_decode($result['category_name'], ENT_QUOTES, 'UTF-8')
				);	
			}
		}
		$this->response->setOutput(json_encode($json));
	}
	
	private function getList() {
		//get data filter
		$data = $this->getData();
		$data['parent_id'] = '0';
		//get url
		$url = $this->url($data);
		
		//get list row	
		$this->data['categories'] = array();		
		$results = $this->model_news_category->getCategories($data);		
		foreach ($results as $result) {
			$action = array();			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('news/category/update', 'token=' . $this->session->data['token']. $url . '&category_id=' . $result['category_id'], 'SSL')
			);					
			$this->data['categories'][] = array(
				'category_id' 	=> $result['category_id'],
				'category_name' => $result['name'],
				'type_id'     	=> $result['type_id'],	
				'status'      	=> $result['status'],
				'sort_order'  	=> $result['sort_order'],
				'selected'    	=> isset($this->request->post['selected']) && in_array($result['category_id'], $this->request->post['selected']),
				'action'      	=> $action
			);
		}
   		
		$this->data['href_sort_name'] = $this->url->link('news/category', 'token=' . $this->session->data['token'] . $this->url($data, "cd.`name`"), 'SSL');		
		$this->data['href_sort_status'] = $this->url->link('news/category', 'token=' . $this->session->data['token'] . $this->url($data, "c.status"), 'SSL');		
		$this->data['href_sort_order'] = $this->url->link('news/category', 'token=' . $this->session->data['token'] . $this->url($data, "c.sort_order"), 'SSL');
		
		$this->load->model('news/type');
		$this->data['news_types'] = $this->model_news_type->getNewsTypes();
		//list status
		$this->data['status_list'] = $this->getStatusList();
		//set breadcrumbs
   		$this->setBreadcrumbs($url);   			
   		$this->data['breadcrumbs'][] = array(
	       	'text'      => $this->language->get('heading_title'),
			'href'      => '#',
	      	'separator' => ' :: '
	   	);		
	   	
	   	//set pagination
		$pagination = new Pagination();
		$pagination->total 	= $this->model_news_category->getTotalCategory($data);
		$pagination->page 	= $data['page'];
		$pagination->limit 	= $this->config->get('config_admin_limit');
		$pagination->text 	= $this->language->get('text_pagination');
		$pagination->url 	= $this->url->link('news/category', 'token=' . $this->session->data['token'] . $url, 'SSL');
				 
		$this->data['pagination'] = $pagination->render();

		//set default value
		$this->data['filter_name'] 		= $data['filter_name'];
		$this->data['filter_type']		= $data['filter_type'];
		$this->data['filter_status'] 	= $data['filter_status'];

		$this->data['sort'] 			= $data['sort'];
		$this->data['order'] 			= $data['order'];
		
		//----------
		$this->data['insert'] = $this->url->link('news/category/insert', 'token=' . $this->session->data['token']. $url, 'SSL');
		$this->data['delete'] = $this->url->link('news/category/delete', 'token=' . $this->session->data['token']. $url, 'SSL');
		$this->data['token'] = $this->session->data['token'];
 		$this->setMessage();
 		$this->setTemplate('news/category_list.tpl');
	}
	
	private function getForm(){
		$this-> setMessage();
  		//get data filter
		$data = $this->getData();
		
		//get url
		$url = $this->url($data);
		//set breadcrumbs
		$this->setBreadcrumbs($url);
  		$this->data['breadcrumbs'][] = array(
	       	'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('news/category', 'token=' . $this->session->data['token'].$url, 'SSL'),
	      	'separator' => ' :: '
	   	);
	   	$this->data['breadcrumbs'][] = array(
	       	'text'      => isset($this->request->get['category_id']) ? $this->language->get('heading_update'): $this->language->get('heading_insert'),
			'href'      => '#',
	      	'separator' => ' :: '
	   	);
		
	   	//actiong button
	   	if (!isset($this->request->get['category_id'])) {
			$this->data['action'] = $this->url->link('news/category/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('news/category/update', 'token=' . $this->session->data['token'] . '&category_id=' . $this->request->get['category_id'] . $url, 'SSL');			
		}	
		$this->data['cancel'] = $this->url->link('news/category', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['token'] = $this->session->data['token'];
		
		//get catogory information when update	
		if (isset($this->request->get['category_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$category_info = $this->model_news_category->getCategory($this->request->get['category_id']);
		}	   	
  		//load language
  		$this->load->model('setting/language');		
		$this->data['languages'] = $this->model_setting_language->getLanguages(array('supported' => $this->restaurant->getConfig()->get('config_supported_language')));
		
		//get category description
		if (isset($this->request->post['category_description'])) {
			$this->data['category_description'] = $this->request->post['category_description'];
		} elseif (isset($this->request->get['category_id'])) {
			$this->data['category_description'] = $this->model_news_category->getCategoryDescriptions($this->request->get['category_id']);
		} else {
			$this->data['category_description'] = array();
		}
		
		//list category parrent
		$this->load->model('news/type');
		$this->data['news_type_list'] = $this->model_news_type->getNewsTypes();	
		if (isset($this->request->post['type_id'])) {
			$this->data['type_id'] = $this->request->post['type_id'];
		} elseif (!empty($category_info)) {
			$this->data['type_id'] = $category_info['type_id'];
		} else {
			$this->data['type_id'] = 1;
		}
	
		//input keyword
		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (!empty($category_info)) {
			$this->data['keyword'] = $category_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}
		
				
		//check top
		if (isset($this->request->post['top'])) {
			$this->data['top'] = $this->request->post['top'];
		} elseif (!empty($category_info)) {
			$this->data['top'] = $category_info['top'];
		} else {
			$this->data['top'] = 0;
		}
		
		//input entry_column
		if (isset($this->request->post['column'])) {
			$this->data['column'] = $this->request->post['column'];
		} elseif (!empty($category_info)) {
			$this->data['column'] = $category_info['column'];
		} else {
			$this->data['column'] = 1;
		}
		
		//input order
		if (isset($this->request->post['sort_order'])) {
			$this->data['sort_order'] = $this->request->post['sort_order'];
		} elseif (!empty($category_info)) {
			$this->data['sort_order'] = $category_info['sort_order'];
		} else {
			$this->data['sort_order'] = 0;
		}
		
		//selectbox status
		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (!empty($category_info)) {
			$this->data['status'] = $category_info['status'];
		} else {
			$this->data['status'] = 1;
		}
			
		//set template
		$this->setTemplate('news/category_form.tpl');
	} 
	
	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'news/category')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		foreach ($this->request->post['category_description'] as $language_id => $value) {
			if ((utf8_strlen($value['name']) < 2) || (utf8_strlen($value['name']) > 255)) {
				$this->error['name'][$language_id] = $this->language->get('error_name');
			}
		}
		
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
					
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'news/category')) {
			$this->error['warning'] = $this->language->get('error_permission');
		} 
		if (!$this->error) {
			return true; 
		} else {
			return false;
		}
	}
	
	/********************************************************************************************
	 * Added by cuongnv 09/11/2011
	 * Common screen category
	 *********************************************************************************************/
	private function load(){
		$this->load->language('news/category');
		$this->data = array_merge($this->data, $this->language->getData());				
		$this->load->model('news/category');
	}
	
	private function getData(){
		$page = isset($this->request->get['page']) ? $this->request->get['page'] : 1;
		$data = array(
			'sort'            	=> isset($this->request->get['sort']) ? $this->request->get['sort'] : 'cd.`name`',
			'order'           	=> isset($this->request->get['order']) ? $this->request->get['order'] : 'ASC',
			'page'           	=> $page,
			'start'           	=> ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'           	=> $this->config->get('config_admin_limit'),
		
			'filter_name'	  	=> isset($this->request->get['filter_name']) 	? $this->request->get['filter_name'] 	: null, 
			'filter_type'		=> isset($this->request->get['filter_type']) 	? $this->request->get['filter_type'] 	: null, 
			'filter_status'   	=> isset($this->request->get['filter_status']) 	? $this->request->get['filter_status']	: null
		);
		
		return $data;
	}	
	
	private function url($data = array(), $sort = ''){
		$order = $data['order'];
		if(!empty($sort)){ 
			$sort = $sort; 
			if ($data['order'] == 'ASC') {$order = 'DESC';} else {$order = 'ASC';}
		} else { $sort = $data['sort']; }
			
		$url  = "";
		$url .= '&page='	. $data['page'];
		$url .= '&sort='	. $sort;
		$url .= '&order=' 	. $order;
		
		$url .= $data['filter_name'] 		!= null ? '&filter_name='	.$data['filter_name']	: "";
		$url .= $data['filter_type']		!= null ? '&filter_type='	.$data['filter_type']	: "";
		$url .= $data['filter_status']		!= null ? '&filter_status='	.$data['filter_status']	: "";
		
		return $url;
	} 
	
	private function setBreadcrumbs($url){
		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'].$url, 'SSL'),
      		'separator' => false
   		);	
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_news'),
			'href'      => '#',
      		'separator' => ' :: '
   		);   			
	}
	
	private function setMessage(){
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}	
 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = array();
		}
	}

	private function setTemplate($template_path){
		$this->template = $template_path;
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}
	
	private function getStatusList(){
		$statusArray = array();
   		$statusArray[] = array(
       		'name'	=> $this->language->get('text_enabled'),
      		'value' => '1'
   		);
   		$statusArray[] = array(
       		'name'	=> $this->language->get('text_disabled'),
      		'value' => '0'
   		);
   		
   		return $statusArray;
	}
}
?>